<?php
	/**
	 * TEMPLATE CONTROLLERS
	 * @author Tom
	 * @package XiTemplate
	 * @package Langues
	 */
	include_once(dirname(__FILE__).'/../config/LaXia.config.php');/** not using yet */
	include_once(dirname(__FILE__).'/../views/XiTemplate.class.php');

	class LaXia extends XiTemplate
	{
		private $block;
		private $lang;
		private $db;
		private $var;

		function __construct($var, $data = false, $lang = false, $db = false, $block = false)
		{

			$this->block = $block;
			$this->lang = $lang;
			$this->db = $db;
			$this->var = $var;
			$this->setTemp($data);
		}

		//set template
		//variable store site config in VxTemplate.config.php
		private function setTemp($data = false)
		{
			$var = $this->var;
			if(!is_array($var))
			{
				parent::__construct(dirname(__FILE__).PATH.$var);
			}
			else
			{
				parent::__construct(dirname(__FILE__).PATH.$var['TEMPLATE']);
				foreach($var as $key => $value)
				{
					if($key!='TEMPLATE')
					{
						$offset = new VxTemplate($value, false, $this->lang, $this->db, $this->block);
						$this->assign($key, $offset->text('main'));
					}
				}
				$var = $var['TEMPLATE'];
			}

			if($this->lang)
			{
				$var = explode('.', $var);
				$var = $var[0].'_page';
				$this->db->setQuery("SELECT ".$var." FROM language WHERE id=".$this->lang);
				if($this->db->checkData())
				{
					$language = $this->db->fetchArray();
					$languages = explode(';v:x;', $language[0][$var]);
					foreach($languages as $value)
					{
						$phrase = explode(':v:x:', $value);
						$this->assign($phrase[0], $phrase[1]);
					}
				}
			}

			if($data)
			{
				foreach($data as $key => $value)
				{
					$this->assign($key, $value);
				}
			}
			if(!$this->block)
				$this->parse('main');
		}

		//create illusion of block
		public function illusion($data = false, $block = false)
		{
			$var = $this->var;
			if(is_array($var))
				$var = $var['TEMPLATE'];

			if($this->lang)
			{
				$var = explode('.', $var);
				$var = $var[0].'_page';
				$this->db->setQuery("SELECT ".$var." FROM language WHERE id=".$this->lang);
				if($this->db->checkData())
				{
					$language = $this->db->fetchArray();
					$languages = explode(';v:x;', $language[0][$var]);
					foreach($languages as $value)
					{
						$phrase = explode(':v:x:', $value);
						$this->assign($phrase[0], $phrase[1]);
					}
				}
			}

			if($data)
			{
				foreach($data as $key => $value)
				{
					$this->assign($key, $value);
				}
			}

			if(!$block)
				if(!$this->block)
					$this->parse('main');
				else
					$this->parse($this->block);
			else
				$this->parse($block);
		}

	}

?>